package com.saturn.titan.web.controller.baseinfo;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.saturn.titan.core.bean.BaseArea;
import com.saturn.titan.core.bean.BaseAreaTreeData;
import com.saturn.titan.core.comm.CRMCache;
import com.saturn.titan.core.comm.ResponseBean;
import com.saturn.titan.core.service.AreaService;
import com.saturn.titan.web.utils.CRMUtil;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/area")
public class AreaController {

	@Autowired
	private AreaService areaService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) throws ParseException {
		return "baseinfo/area/edit";
	}
	
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean delete(@RequestParam(value="id")Long id,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			//删除本记录及其子记录
			areaService.deleteNode(id);
			
			rb.setResult(ResponseBean.RESULT_OK);
		} catch (Exception e) {
			e.printStackTrace();
			rb.setResult(ResponseBean.RESULT_NG);
			rb.setMeg(e.getMessage());
		}
		return rb;
	}
	
	@RequestMapping(value = "add",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean add(@Valid
			BaseAreaTreeData area,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			BaseArea a = new BaseArea();
			a.setAreaName(area.getAreaName());
			a.setRemark(area.getRemark());
			a.setCrtDate(new Date());
			a.setCrtOpr(CRMUtil.getCurrentUser().id);
			a.setParentId(area.getParentId());
			areaService.add(a);
			rb.setResult(ResponseBean.RESULT_OK);
			rb.setData(a);
		} catch (Exception e) {
			e.printStackTrace();
			rb.setResult(ResponseBean.RESULT_NG);
			rb.setMeg(e.getMessage());
		}
		return rb;
	}
	
	@RequestMapping(value = "update",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean update(@Valid
			BaseAreaTreeData area,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			BaseArea a = new BaseArea();
			a.setId(area.getId());
			a.setAreaName(area.getAreaName());
			a.setRemark(area.getRemark());
			a.setParentId(area.getParentId());
			a.setVersion(area.getVersion());
			a.setLstEdtDate(new Date());
			a.setLstEdtOpr(CRMUtil.getCurrentUser().id);
			
			areaService.update(a);
			rb.setResult(ResponseBean.RESULT_OK);
			rb.setData(a);
		} catch (Exception e) {
			e.printStackTrace();
			Throwable tmp = CRMUtil.getDataHaveChangedException(e);
			if(tmp != null){
				rb.setMeg(tmp.getMessage());
			}else{
				rb.setMeg(e.getMessage());
			}
			rb.setResult(ResponseBean.RESULT_NG);
		}
		return rb;
	}
	
	@RequestMapping(value = "selectArea")
	@ResponseBody
	public Map<String,Object> selectArea(@RequestParam(value="areaName") String areaName,
			@RequestParam(value="remark") String remark) throws ParseException {
		
		BaseArea ba = new BaseArea();
		if(!CRMUtil.blankStr(areaName)){
			ba.setAreaName(areaName);
		}
		if(!CRMUtil.blankStr(remark)){
			ba.setRemark(remark);
		}
		
		List<BaseArea> baseAreas = areaService.findArea(ba);
		List<BaseAreaTreeData> result = Lists.newArrayList();
		BaseAreaTreeData tmp = null;
		for(BaseArea area : baseAreas){
			tmp = new BaseAreaTreeData();
			tmp.setId(area.getId());
			tmp.setAreaName(area.getAreaName());
			tmp.setRemark(area.getRemark());
			tmp.setCrtOpr(area.getCrtOpr());
			tmp.setCrtDate(area.getCrtDate());
			tmp.setLstEdtDate(area.getLstEdtDate());
			tmp.setLstEdtOpr(area.getLstEdtOpr());
			tmp.setIconCls("icon-map");
			tmp.set_parentId(area.getParentId());
			tmp.setVersion(area.getVersion());
			tmp.setDisplayCrtOpr(CRMCache.displayOpr(area.getCrtOpr()));
			tmp.setDisplayLstEdtOpr(CRMCache.displayOpr(area.getLstEdtOpr()));
			result.add(tmp);
		}
		
		Map<String,Object> r = Maps.newHashMap();
		r.put("total", result.size());
		r.put("rows", result);
		r.put("foot", "");
		return r;
	}
	
	@RequestMapping(value = "selectAreaSelectOption")
	@ResponseBody
	public Map<String,Object> selectRoleSelectOption() throws ParseException {
		Map<String,Object> r = Maps.newHashMap();
		List<BaseArea> result = areaService.findAllArea();
		r.put("total", result.size());
		r.put("rows", result);
		return r;
	}
}
